
//
// 121213 - AH - Created.
// 130105 - AH - log(boolean, Object, String, String) and
//               rlog(boolean, Object, String, String).
//

package org.japril.jutil.logging;

import org.japril.swing.jutil.logging.ControlLogSinks;


/**
 * <p>
 *   Static methods for doing logging.
 * </p>
 * @author Arne Halvorsen (AH)
 */
public final class Log 
{
  private static LogSink c_ls;
  
  static
  {
    ControlLogSinks sinks=ControlLogSinks.getInstance();
    c_ls=sinks.getCurrentSink();
  }
  
  private Log(){} // Utility pattern dictates private constructor.
  
  /**
   * <p>
   *   Sets 
   *   {@link LogSink} to use.
   * </p>
   * @param ls {@link LogSink} to use. If {@code null} uses a
   *           {@link PrintLogSink}.
   */
  public static void setLogSink(LogSink ls)
  {
    c_ls=(ls==null) ? new PrintLogSink() : ls;
  }
  
  /**
   * <p>
   *   Gets 
   *   {@link LogSink} used.
   * </p>
   * @return {@code LogSink} used.
   */
  public static LogSink getLogSink(){ return c_ls; }
  
  /**
   * <p>
   *   Log.
   * </p>
   * @param src    Either object or class logging, may be {@code null}.
   * @param method Name on method logging, may be {@code null}.
   * @param msg    Log message, may be {@code null}.
   */
  public static void log(Object src, String method, String msg)
  {
    c_ls.log(getSrcClass(src), method, msg);
  }
  
  /**
   * <p>
   *   Log 'raw', that is without using registered 
   *   {@link LogFormater}.
   * </p>
   * @param src    Either object or class logging, may be {@code null}.
   * @param method Name on method logging, may be {@code null}.
   * @param msg    Log message, may be {@code null}.
   */
  public static void rlog(Object src, String method, String msg)
  {
    c_ls.rlog(getSrcClass(src), method, msg);
  }
  
  /**
   * <p>
   *   Log.
   * </p>
   * @param log    If {@code false} no logging will be done for sure as the
   *               {@link LogSink} will not be notified. 
   * @param src    Either object or class logging, may be {@code null}.
   * @param method Name on method logging, may be {@code null}.
   * @param msg    Log message, may be {@code null}.
   */
  public static void log(boolean log, Object src, String method, String msg)
  {
    if (log) c_ls.log(getSrcClass(src), method, msg);
  }
  
  /**
   * <p>
   *   Log 'raw', that is without using registered 
   *   {@link LogFormater}.
   * </p>
   * @param log    If {@code false} no logging will be done for sure as the
   *               {@link LogSink} will not be notified.
   * @param src    Either object or class logging, may be {@code null}.
   * @param method Name on method logging, may be {@code null}.
   * @param msg    Log message, may be {@code null}.
   */
  public static void rlog(boolean log, Object src, String method, String msg)
  {
    if (log) c_ls.rlog(getSrcClass(src), method, msg);
  }
 
  private static Class<?> getSrcClass(Object src)
  {
    if (src==null) return null;
    return src instanceof Class<?> ? (Class<?>)src : src.getClass();
  }
  
}
